﻿using System;
using System.Windows.Forms;

namespace AMM.CustomWinControls {
    public partial class PagingControlEx : UserControl {

        private int _pageCount;

        public delegate void PaginationEventHandler(object sender, PaginationArgs e);
        public event PaginationEventHandler Paging;

        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int Records { get; set; }

        public void OnPaging(object sender, PaginationArgs e) {
            if (this.Paging != null) {
                this.Paging(sender, e);
            }
        }

        public void DataBind() {
            this.CalculatePageCount();
            this.SetupUI();
        }

        public PagingControlEx() {
            this.InitializeComponent();
        }

        private void PagingControlEx_Load(object sender, EventArgs e) {
            this.PageIndex = this.PageIndex == 0 ? 1 : this.PageIndex;
            this.PageSize = this.PageSize == 0 ? 10 : this.PageSize;
            this.Records = 0;
        }

        private void tsPage_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
            this.CalculatePageCount();
            switch (e.ClickedItem.Name) {
                case "tsBtnFirst": this.PageIndex = 1; break;
                case "tsBtnPrevious": this.PageIndex -= 1; break;
                case "tsBtnNext": this.PageIndex += 1; break;
                case "tsBtnLast": this.PageIndex = this._pageCount; break;
            }
            this.Pagiation();
        }

        private void tsTbPage_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Enter) {
                int pageIndex;
                int.TryParse(this.tbPage.Text, out pageIndex);
                this.PageIndex = pageIndex;
                this.CalculatePageCount();
                this.Pagiation();
            }
        }

        private void CalculatePageCount() {
            this._pageCount = this.Records / this.PageSize + (this.Records % this.PageSize > 0 ? 1 : 0);
            this._pageCount = this._pageCount == 0 ? 1 : this._pageCount;
        }

        private void Pagiation() {
            this.SetupUI();
            this.OnPaging(this, new PaginationArgs() {
                PageCount = this._pageCount,
                PageIndex = this.PageIndex,
                PageSize = this.PageSize
            });
        }

        private void SetupUI() {
            this.PageIndex = this.PageIndex <= 0 ? 1 : this.PageIndex;
            this.PageIndex = this.PageIndex > this._pageCount ? this._pageCount : this.PageIndex;
            this.tbPage.Text = this.PageIndex.ToString();
            this.lblPageCount.Text = string.Format("/ {0}  共有 {1} 条记录", this._pageCount, this.Records);
            this.tsBtnFirst.Enabled = this.PageIndex != 1;
            this.tsBtnPrevious.Enabled = this.PageIndex > 1;
            this.tsBtnNext.Enabled = this.PageIndex < this._pageCount;
            this.tsBtnLast.Enabled = this.PageIndex != this._pageCount;
        }
    }

    public class PaginationArgs : EventArgs {
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public int PageCount { get; set; }
    }
}
